<?php
namespace App\Admin\Controllers;

use App\Models\BoxUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Support\Str;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;

class BoxUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::js('public\image-preview.js');
        return Grid::make(new BoxUser(), function (Grid $grid) {
            $grid->quickSearch('account', 'username')->placeholder('搜索...')->auto(true);
            $grid->column('id')->sortable();
            $grid->column('account');
            $grid->column('username');
            $grid->column('avatar')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, '/uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });

            $grid->column('class')->using(['1' => '小班', '2' => '中班', '3' => '大班'])->badge([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default    
                1 => 'primary',
                2 => 'danger',
                3 => 'success',
            ]);
            $grid->column('integral');
            $grid->column('balance');
            $grid->column('createtime');

            // $grid->filter(function (Grid\Filter $filter) {
            //     $filter->equal('id');
            // });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BoxUser(), function (Show $show) {
            $show->field('id');
            $show->field('account');
            $show->field('username');
            $show->field('avatar')->image('http://laraveDact.com/', 100, 100);
            $show->field('class')->using(['1' => '小班', '2' => '中班', '3' => '大班']);
            $show->field('integral');
            $show->field('balance');
            $show->field('createtime');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BoxUser(), function (Form $form) {
            $form->display('id');
            $form->text('account')->required();
            $form->text('username')->required();
            $form->password('password')->required();
            $form->image('avatar')->disk('public')->accept('jpg,png,gif,jpeg')->maxSize(512)->required()->autoUpload()->help('大小不要超过512K');
            $form->radio('class')->required()->options([1 => '小班', 2 => '中班', 3 => '大班'])->default(1);
            $form->number('integral')->required();
            $form->number('balance')->required();
            $form->date('createtime')->required()->format('YYYY-MM-DD')->default(date('Y-m-d'));
        });
    }
}
